let content=""
Page({
  data: {
    z_URL:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/image/植物图片/",
    y_URL:"cloud://test-hf5g2.7465-test-hf5g2-1304459590/image/药物图片/",
    z_imgURL:"",
    y_imgURL:"",
    notice:0,
    showIndex:true,
    showInput:false,
    preview:false,
    pre_index:0,
    rembNum:0,
    number:0,
    index:0,
    data_index:0,  // 题目序列
    reciteList:[],//背药序列
    yesList: [], // 认识的序列
    notSureList: [], // 不确定的序列
    noList:[]//不认识的序列
  },

  onLoad: function (options) {
    let that=this;
    wx.setNavigationBarTitle("专业背诵") // 动态设置导航条标题
    that.setData({
      rembNum:options.rembNum,
      data_index:options.data_index,
      number:options.number,
    })
      let rembNum=Number(that.data.rembNum)
      let data_index=Number(that.data.data_index)
      wx.cloud.database().collection("Dictionary").skip(data_index).limit(rembNum).get()
      .then(res=>{
        console.log(res)
          that.setData({
            reciteList:res.data,
            z_imgURL:that.data.z_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg",
            y_imgURL:that.data.y_URL+res.data[that.data.index].chapter+"/"+res.data[that.data.index].section+"/"+res.data[that.data.index].name+".jpg"
          })
      }) 
      .catch(res=>{
        console.log(res)
      })
    //   {
    //   success(res)
    //   {
    //     console.log(res)
    //     that.setData({
    //       reciteList:res.data
    //     })
    //   },
    // })
  },
  onShow(){

  },
  onUnload(){
  },
  // 认识事件
  toYesPage: function (e) {
    let that=this
    if (that.data.index < that.data.reciteList.length) {
      // 渲染下一题
      that.setData({
        showInput:true,
        notice:false
      })
    } 
  },
  // 不认识事件
  toNoPage(){
    let that=this
    that.setData({
      showIndex:false,
      // notice:false
    })
  },
  toNotice(){
    let that=this
    if(this.data.notice<3){
      if(that.data.index<that.data.reciteList.length){
        that.setData({
          notice:that.data.notice+1
        })
      }
    }else{
      that.setData({
        // index: that.data.index + 1,
        // rembNum:that.data.rembNum - 1,
        showIndex:false,
        notice:0
      })
    }
    
  },
  nextSubmit(){
    let that=this
    let index=that.data.index
    let number=that.data.number
    let data_index=that.data.data_index
     if (that.data.index < that.data.reciteList.length-1) {
        // 渲染下一题
          that.setData({
            index: that.data.index + 1,
            showIndex:true,
            showInput:false,
            rembNum: that.data.rembNum-1,
            z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
            y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
          })
      }else if(that.data.index==that.data.reciteList.length-1)
      {
        that.data.data_index=that.data.number;
        wx.redirectTo({
          url: '../results_tcm/results_tcm?updata=true',
        })
      }
      // if(that.data.index==that.data.reciteList.length)
      // {
      //   that.setData({
      //     data_index:Number(data_index)
      //   })
      //   // data_index=index+data_index+1
      //   wx.redirectTo({
      //     url: '../results_tcm/results_tcm?data_index='+data_index+'&number='+number,
      //   })
      // }
      else{
      wx.redirectTo({
            url: '../results_tcm/results_tcm?update=true',
          })
    }
  },
  getValue(e){
    content=e.detail.value
  },
  submitButton(){
    let that=this
    var reciteList=that.data.reciteList
    var index=that.data.index
    var data_index=that.data.data_index
    var number=that.data.number
    if(reciteList[index].name==content)
    {
      wx.showToast({
        title: '回答正确',
      })
      if (that.data.index < that.data.reciteList.length-1) {
        // 渲染下一题
          that.setData({
            index: that.data.index + 1,
            showIndex:true,
            showInput:false,
            rembNum: that.data.rembNum-1,
            z_imgURL:that.data.z_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg",
            y_imgURL:that.data.y_URL+that.data.reciteList[that.data.index+1].chapter+"/"+that.data.reciteList[that.data.index+1].section+"/"+that.data.reciteList[that.data.index+1].name+".jpg"
          })
      }else if(that.data.index==that.data.reciteList.length-1)
      {
        that.data.data_index=that.data.number;
        wx.redirectTo({
          url: '../results_tcm/results_tcm',
        })
      }
    }
    else
    {
      wx.showToast({
        title: '回答错误',
        icon:"none"
      })
      that.setData({
        showIndex:false,
        showInput:false
      })
    }
  },
  toNoButton()
  {
    let that=this
    that.setData({
      showIndex:false,
      showInput:false
    })
  },
  z_exchangeImage(e){
    let that=this
    let URL=that.data.z_imgURL.split('.')
    URL[2]="webp"
    URL=URL.join('.')
    console.log(e)
    that.setData({
      z_imgURL:URL
    })
  },
  y_exchangeImage(e){
    let that=this
    let URL=that.data.y_imgURL.split('.')
    URL[2]="webp"
    URL=URL.join('.')
    console.log(e)
    that.setData({
      y_imgURL:URL
    })
  }
})
